﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace IPConfig
{
    public static class NetworkHelper
    {
        /// <summary>
        /// 修改本机IP
        /// </summary>
        public static void SetNetworkAdapter(IPConfigModel model)
        {
            string error = CheckIPConfig(model);
            if (!string.IsNullOrEmpty(error))
            {
                throw new Exception("IP配置错误，无法切换！ " + error);
            }
            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (!(bool)mo["IPEnabled"])
                    continue;
                string[] addresses = (string[])mo["IPAddress"];
                if (addresses[0].Contains("xxx.xxx.xxx.xxx"))
                {
                    Console.WriteLine("不修改保护的地址");
                    continue;
                }
                Console.WriteLine("开始修改");
                try
                {
                    //设置ip地址和子网掩码 
                    inPar = mo.GetMethodParameters("EnableStatic");
                    inPar["IPAddress"] = new string[] { model.IP };
                    inPar["SubnetMask"] = new string[] { model.ZW };//"255.255.255.0"
                    outPar = mo.InvokeMethod("EnableStatic", inPar, null);
                    //设置网关地址 
                    inPar = mo.GetMethodParameters("SetGateways");
                    inPar["DefaultIPGateway"] = new string[] { model.WG };
                    outPar = mo.InvokeMethod("SetGateways", inPar, null);
                }
                catch (Exception e)
                {
                    throw e;
                }
                //设置DNS 
                inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                inPar["DNSServerSearchOrder"] = new string[] { model.DNSF };
                outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                break;
            }
        }

        public static void SetNetworkDHCP()
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (!(bool)mo["IPEnabled"]) continue;
                mo.InvokeMethod("SetDNSServerSearchOrder", null);
                mo.InvokeMethod("EnableStatic", null);
                mo.InvokeMethod("SetGateways", null);
                mo.InvokeMethod("EnableDHCP", null);
                break;

            }
        }
        public static string CheckIPConfig(IPConfigModel model)
        {
            Regex regex = new Regex("^((/d|[1-9]/d|1/d/d|2[0-4]/d|25[0-5])/.){3}(/d|[1-9]/d|1/d/d|2[0-4]/d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$/");
            if (!string.IsNullOrEmpty(model.IP))
            {
                if (regex.IsMatch(model.IP))
                {
                    return "IP地址输入不正确！";
                }
            }
            else
            {
                return "IP地址不能为空！";
            }
            if (!string.IsNullOrEmpty(model.ZW))
            {
                if (regex.IsMatch(model.ZW))
                {
                    return "子网掩码输入不正确！";
                }
            }
            else
            {
                return "子网掩码不能为空！";
            }
            if (!string.IsNullOrEmpty(model.WG))
            {
                if (regex.IsMatch(model.WG))
                {
                    return "默认网关输入不正确！";
                }
            }
            else
            {
                return "默认网关不能为空！";
            }
            if (!string.IsNullOrEmpty(model.DNSF))
            {
                if (regex.IsMatch(model.IP))
                {
                    return "首选DNS服务器输入不正确！";
                }
            }
            else
            {
                return "首选DNS服务器输入不能为空！";
            }
            if (!string.IsNullOrEmpty(model.DNSF))
            {
                if (regex.IsMatch(model.IP))
                {
                    return "备选DNS服务器输入不正确！";
                }
            }
            return string.Empty;
        }
    }
}
